﻿namespace dk.Helper.Base.Core
{
    public class WorldMapFilter : IWorldMapFilter
    {
        private readonly IWorld world;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        /// <param name="world">The world.</param>
        public WorldMapFilter(IWorld world)
        {
            this.world = world;
            ShowAbandonedVillages = true;
            ShowPlayersWithoutAlly = true;
            PlayerPoints = new Interval(0, 0);
            PlayerPoints.IsDefined = false;
            VillagePoints = new Interval(0, 0);
            VillagePoints.IsDefined = false;
        }

        #region IWorldMapFilter Members

        /// <summary>
        /// Determines whether village is satisfying specified conditions or not.
        /// </summary>
        /// <param name="village">The village.</param>
        /// <returns>
        ///   <c>true</c> if conditions are met; otherwise, <c>false</c>.
        /// </returns>
        public bool IsSatisfyingConditions(IVillage village)
        {
            bool result = true;
            if (VillagePoints.IsDefined)
                result = VillagePoints.InRange(village.Points);

            if (village.PlayerID == 0)
                result = result && ShowAbandonedVillages;
            else
            {
                IPlayer player = world.Players.PlayersByID[village.PlayerID];
                if (PlayerPoints.IsDefined)
                {
                    result = result && PlayerPoints.InRange(player.Points);
                }
                if (player.AllyID == 0)
                {
                    result = result && ShowPlayersWithoutAlly;
                }
            }

            return result;
        }

        /// <summary>
        /// Gets or sets the village points.
        /// </summary>
        /// <value>
        /// The village points.
        /// </value>
        public Interval VillagePoints { get; set; }

        /// <summary>
        /// Gets or sets the player points.
        /// </summary>
        /// <value>
        /// The player points.
        /// </value>
        public Interval PlayerPoints { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether abandonned villages should be shown or not.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if abandonned villages should be shown; otherwise, <c>false</c>.
        /// </value>
        public bool ShowAbandonedVillages { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether players without ally should be shown or not.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if players without ally should be shown; otherwise, <c>false</c>.
        /// </value>
        public bool ShowPlayersWithoutAlly { get; set; }

        #endregion
    }
}